package com.snowRemovingUser.controller;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.ip.AddressUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.snowRemovingUser.domain.TOperStatistics;
import com.snowRemovingUser.domain.TOperation;
import com.snowRemovingUser.mapper.TOperStatisticsMapper;
import com.snowRemovingUser.mapper.TOperationMapper;
import com.snowRemovingUser.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/setOperationLog")
public class OperationLogController {

    @Autowired
    private TOperationMapper tOperationMapper;

    @Autowired
    private TOperStatisticsMapper tOperStatisticsMapper;

    @Autowired
    private RedisCache redisCache;


    /**
     * 用户的操作记录
     * @return
     */
    @Anonymous
    @GetMapping("/operation")
    public AjaxResult EventHandling(String Login , String operType){


        Object account = redisCache.getCacheObject(operType);

        if (account==null){

            //操作记录
            TOperation tOperation = new TOperation();
            //操作记录统计
            TOperStatistics tOperStatistics = new TOperStatistics();

            tOperation.setOperAccount(Login);

            Integer operInt = Integer.valueOf(operType);
            tOperation.setOperType(operInt);
            //操作ip
            final String ip = IpUtils.getIpAddr();
            tOperation.setOperIp(ip);

            //操作地点
            String address = AddressUtils.getRealAddressByIP(ip);
            tOperation.setOperLocation(address);

            //操作时间
            Date date = new Date();
            tOperation.setOperTime(date);

            // 将operType 放入redis中 为接口做幂等性
            redisCache.setCacheObject(operType,operType,1, TimeUnit.MINUTES);


            //加入操作日志中
            tOperationMapper.insertTOperation(tOperation);


            //查询今天和查询类型是否有值
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String eventTimeStr = simpleDateFormat.format(date);
            Date today = DateUtil.toDate(eventTimeStr, "yyyy-MM-dd");
            TOperStatistics OperStat = tOperStatisticsMapper.selectTOperStatisticsByToDay(operInt, today);

            //没有值就直接添加
            if (OperStat==null){

                tOperStatistics.setOperType(operInt);
                tOperStatistics.setOperNo(1);
                tOperStatistics.setOperTime(today);
                tOperStatisticsMapper.insertTOperStatistics(tOperStatistics);

            //有值就在统计次数+1 再更新
            }else {

                OperStat.setOperNo(OperStat.getOperNo()+1);
                tOperStatisticsMapper.updateTOperStatistics(OperStat);

            }


            return AjaxResult.success("记录日志成功");

        }else {

            redisCache.setCacheObject(operType,operType,1, TimeUnit.MINUTES);

            return AjaxResult.success("重复点击操作，不记录日志");
        }
    }




}
